/*
 * Ext GWT - Ext for GWT Copyright(c) 2007, 2008, Ext JS, LLC. licensing@extjs.com http://extjs.com/license
 */
package com.netbetter.langzheng.plugin.fileManager.model;

import com.extjs.gxt.ui.client.data.BaseModelData;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;

/**
 * 文件对象，通过json格式数据映射为java对象
 * 
 * @author 嘉陵江里捉鱼
 */
public class FileModel extends BaseModelData {

    @Override
    public boolean equals(Object obj) {
        if (obj != null) {
            if (obj instanceof FileModel) {
                FileModel rightObject = (FileModel) obj;
                if (rightObject.getId() == this.getId()) {
                    return true;
                }
            }
        }
        return false;
    }

    private static final long serialVersionUID = 1L;

    // private static final DateTimeFormat formate =
    // DateTimeFormat.getFormat("yyyy/MM/dd 24hh:mm:ss");

    public void setCreateTime(String createTime) {
        set("createTime", createTime);
    }

    public void setAccessTime(String accessTime) {
        set("accessTime", accessTime);
    }

    public void setModifyTime(String modifyTime) {

        set("modifyTime", modifyTime);
    }

    private String getCurrentTime(String columnName) {
        String value = get(columnName).toString();
        if (value.length() > 20) {
            return value.substring(0, 20);
        }
        return "";
    }

    public String getAccessTime() {
        return getCurrentTime("accessTime");
    }

    public String getCreateTime() {

        return getCurrentTime("createTime");
    }

    public String getModifyTime() {
        return getCurrentTime("modifyTime");
    }

    public void setId(int id) {
        set("id", id);
    }

    public int getId() {
        return get("id");
    }

    public void setName(String name) {
        set("name", name);
    }

    public void setSize(int size) {
        set("size", size);
    }

    public int getSize() {
        return get("size");
    }

    public String getName() {
        return get("name");
    }

    public void setIsDirectory(boolean isDirectory) {
        set("isDirectory", isDirectory);
    }

    public boolean getIsDirectory() {
        return get("isDirectory");
    }

    public void setFSize(int size) {
        set("fSize", getFormateSize());
    }

    public String getFSize() {
        return get("fSize");
    }

    private String getFormateSize() {
        double size = getSize();
        NumberFormat format = NumberFormat.getFormat("#0.00");
        if (size > 1024 * 1024) {
            return format.format(size / (1024 * 1024)) + "M";
        } else if (size > 1024) {
            return format.format(size / (1024)) + "KB";
        } else {
            return size + "B";
        }

    }

    public void setIconName(String name) {
        if (getIsDirectory()) {
            set("iconName", "<img src='/images/gxt/icons/folder-closed.gif'/> " + name);
        } else {
            String prefix = getPrefix();
            String[] prefixs = new String[] { "zip", "txt", "php", "flash", "exe", "xls", "doc", "cs", "c++" };
            boolean isExist = false;
            for (int i = 0; i < prefixs.length; i++) {
                if (prefixs[i].equalsIgnoreCase(prefix)) {
                    isExist = true;
                }
            }
            if (!isExist) {
                prefix = "tux";
            }
            set("iconName", "<img src='/images/icons/page_white_" + prefix.toLowerCase() + ".png'/> " + name);
        }

    }

    public String getIconName() {
        return get("iconName");
    }

    public String getType() {

        return get("type");
    }

    public void setType(String name) {
        String value = "";
        if (getIsDirectory()) {
            value = "文件夹";
        } else {
            value = getPrefix() + "类型";
        }
        set("type", value);
    }

    public String getPrefix() {
        String fileName = getName();
        int pos = fileName.lastIndexOf(".");
        if (pos >= 0 && pos < fileName.length()) {
            String prefix = fileName.substring(pos + 1, fileName.length());
            return prefix;
        }
        return "";
    }

    public JSONObject getJSONObject() {
        JSONObject retval = new JSONObject();
        retval.put("id", new JSONNumber(getId()));
        retval.put("name", new JSONString(getName()));
        return retval;
    }

}
